<template>
  <div class="content-warpper">
    <div class="left-tree">
      <el-input
        placeholder="输入主题名称"
        v-model="filterText"
        class="form-input"
        suffix-icon="el-icon-search"
        size="small"
      >
      </el-input>
      <el-tree
        v-if="isShowTree"
        class="filter-tree"
        :data="subjectList"
        :props="defaultProps"
        @node-click="handleNodeClick"
        :default-expand-all="true"
        :filter-node-method="filterNode"
        node-key="id"
        :default-checked-keys="[selectedNodeID]"
        :highlight-current="true"
        ref="tree"
      ></el-tree>
    </div>
    <div class="server-main right-content">
      <el-form :inline="true" v-model="modelData" class="search-form">
        <el-form-item label="模型名称" class="form-input">
          <el-input
            placeholder="请输入模型名称"
            v-model.trim="modelData.modelName"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button id="btn" size="small" type="primary" @click="onSubmit(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <el-empty
        description="暂无逻辑模型"
        v-if="modelList.length === 0"
      ></el-empty>
      <div class="server-table" v-if="modelList.length > 0">
        <div class="table-title">
          <!-- <span class="table-title-text">逻辑模型</span> -->
          <div>
            <!-- <el-button
              :disabled='!addAble'
              type="primary"
              size="small"
              @click="openCreateOntology()"
            >创建模型</el-button>
            <el-button
              type="primary"
              size="small"
              @click="openAnalysePop()"
            >生成物理模型</el-button> -->
          </div>
        </div>
        <el-table
          :data="modelList"
          class="km-table"
          highlight-current-row
          @current-change="handleImportChange"
        >
          <!-- <el-table-column
            type="selection"
            width="55"
          >
          </el-table-column> -->
          <el-table-column
            type="index"
            prop="logicalModelId"
            label="ID"
            width="50"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="modelCode"
            label="模型编码"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="modelName"
            label="模型名称"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="description"
            label="模型说明"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="themeName"
            label="所属主题"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="version"
            label="版本"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="modelStatus"
            label="状态"
            align="center"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <i
                class="little-circle-yellow"
                v-if="scope.row.modelStatus === 0"
              ></i>
              <span v-if="scope.row.modelStatus === 0">编辑中</span>
              <i
                class="little-circle-green"
                v-if="scope.row.modelStatus === 1"
              ></i>
              <span v-if="scope.row.modelStatus === 1">已发布</span>
              <i
                class="little-circle-red"
                v-if="scope.row.modelStatus === 2"
              ></i>
              <span v-if="scope.row.modelStatus === 2">已停用</span>
            </template>
          </el-table-column>

          <!-- <el-table-column
            label="操作"
            align="center"
            width="250"
          >
            <template slot-scope="scope">
              <el-link
                :underline="false"
                type="primary"
                class="ontology-edit"
                @click.native.prevent="goManagement(scope.row)"
              >查看</el-link>
            </template>
          </el-table-column> -->
        </el-table>
        <el-pagination
          class="km-pagination"
          background
          :small="true"
          layout="total, prev, pager, next,sizes, jumper"
          :total="total"
          :hide-on-single-page="!total"
          :page-size.sync="modelData.pageSize"
          :pager-count="7"
          :page-sizes="[10, 20, 30]"
          :current-page="modelData.pageNum"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        >
        </el-pagination>
        <!-- <el-dialog
          :title="dialogConfig[dialogType].title"
          :visible.sync="dialogFormVisible"
          @close="afterDialogClose"
        >
          <el-form
            :model="modelParams"
            :rules="rules"
            ref="dialogForm"
            label-position="right"
            label-width="120px"
          >
            <el-form-item
              label="模型编码:"
              prop="modelCode"
              class="dialog-inline"
            >
              <el-input v-model.trim="modelParams.modelCode"></el-input>
            </el-form-item>
            <el-form-item
              label="模型名称:"
              prop="modelName"
              class="dialog-inline"
            >
              <el-input v-model.trim="modelParams.modelName"></el-input>
            </el-form-item>
            <el-form-item
              label="模型说明:"
              prop="description"
              class="dialog-inline"
            >
              <el-input
                type="textarea"
                v-model.trim="modelParams.description"
                :autosize="textSizeConfig"
              >
              </el-input>
            </el-form-item>
          </el-form>
          <div
            slot="footer"
            class="dialog-footer"
          >
            <el-button @click="resetDialogForm">取 消</el-button>
            <el-button
              type="primary"
              @click="submitDialogForm"
              :loading="loading"
            >确 定</el-button>
          </div>
        </el-dialog> -->
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "importLogicModel",
  components: {},
  data: () => {
    return {
      modelId: "", // 属性 id
      isShowTree: false, //是否显示树
      selectedNodeID: -1, //树选中节点id
      modelData: {
        modelName: "", //查询--模型名称
        modelStatus: 1, //查询--状态
        pageNum: 1, // 页码
        pageSize: 10, // 每页条数
      },
      modelList: [], // 逻辑模型列表
      subjectList: [], //主题树列表
      filterText: "", //树查询关键字
      defaultProps: {
        //树-展示属性设置
        children: "children",
        label: "label",
      },
      addAble: false, //逻辑模型是否可创建
      dialogFormVisible: false, //创建、编辑弹窗默认状态
      modelParams: {
        //新建模型模板
        modelCode: "", // 模型编码
        modelName: "", // 模型名称
        description: "", // 模型说明
      },
      dialogType: "ADD", //默认弹窗类型--创建模型（edit,编辑模型）
      dialogConfig: {
        //不同类型弹窗标题及方法
        ADD: {
          title: "创建逻辑模型",
          funcName: "createModel",
        },
        EDIT: {
          title: "编辑逻辑模型",
          funcName: "editModel",
        },
      },
      //   rules: {
      //     //弹窗表单项规则
      //     modelCode: [
      //       {
      //         required: true,
      //         message: "请输入模型编码",
      //         trigger: "blur",
      //       },
      //     ],
      //     modelName: [
      //       {
      //         required: true,
      //         message: "请输入模型名称",
      //         trigger: "blur",
      //       },
      //     ],
      //     description: [
      //       {
      //         required: false,
      //         message: "请输入模型描述",
      //         trigger: "blur",
      //       },
      //     ],
      //   },
      textSizeConfig: {
        //texterea设置4-10行
        minRows: 4,
        maxRows: 10,
      },
      loading: false,
      total: 0, // 数据总条数
      dialogVisible: false,
      currentRow: {}, //列表选中模型
    };
  },
  computed: {
    ...mapState({
      selectedID: (state) => state.modelDesign.selectedNodeID,
      selectedNode: (state) => state.modelDesign.selectedNode,
      selectedData: (state) => state.modelDesign.selectData,
    }),
  },
  created() {
    this.getSubjectList();
  },
  watch: {
    filterText(val) {
      //树结构查询关键字
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    //创建模型--选择方式回调
    handleCommand(command) {
      this.$message("click on item " + command);
    },
    //树结构，筛选方法
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    //点击树节点-回调
    handleNodeClick(data) {
      this.selectedNodeID = data.id;
      this.$store.commit("modelDesign/save_selectedNode", data); //传递选中节点
      this.$store.commit(
        "modelDesign/save_selectedNodeID",
        this.selectedNodeID
      ); //传递选中节点id
      if (!data.children || data.children.length === 0) {
        this.addAble = true; //底层节点，可创建新模型
        this.getModelList(data.id);
      } else {
        this.addAble = false;
        this.getModelList(data.id);
      }
    },
    // 查询
    onSubmit(pageNum) {
      this.modelData.pageNum = pageNum ? pageNum : this.modelData.pageNum;
      this.getModelList(this.selectedNodeID);
    },
    // 展示树
    showTree(ID) {
      this.isShowTree = true;
      this.selectedNodeID = ID;
      this.getModelList(ID);
      this.selectedID != -1
        ? this.handleNodeClick(this.selectedNode)
        : this.handleNodeClick(this.subjectList[0]);
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.selectedNodeID); //一定要加这个选中了否则样式没有出来
      });
    },
    // 获取主题列表
    getSubjectList() {
      this.$axios
        .post(this.$api.modelDesign.logicModel.getSubjectList, {})
        .then((res) => {
          let data = res.data.data;
          this.subjectList = data;
          if (data.length > 0) {
            this.selectedID != -1
              ? this.showTree(this.selectedID)
              : this.showTree(data[0].id);
          }
          if (!data[0].children || data[0].children.length === 0)
            this.addAble = true;
        })
        .catch((err) => {
          console.error(err);
          this.$message.error(err.data.message);
        });
    },
    // 获取模型列表 --- 只显示状态为已发布的
    getModelList(ID) {
      let themeId = { themeId: ID };
      let params = { ...this.modelData, ...themeId };
      this.$axios
        .post(this.$api.modelDesign.logicModel.getModelList, params)
        .then((res) => {
          let data = res.data.data.list;
          this.modelList = data;
          this.total = res.data.data.totalCount;
        })
        .catch((err) => {
          this.$message.error(err.data.message);
        });
    },
    // 模态框关闭后
    afterDialogClose() {
      this.$refs.dialogForm && this.$refs.dialogForm.resetFields();
    },
    // 修改当前页码
    handleCurrentChange(page) {
      this.modelData.pageNum = page;
      this.getTableList();
    },
    //修改当前页面容量
    handleSizeChange(page) {
      this.modelData.pageSize = page;
      this.getTableList();
    },
    //获取表格选中项
    handleImportChange(val) {
      this.currentRow = val;
      this.$emit("currentRow", val);
    },
    //管理---跳转到模型管理页面
    goManagement(item) {
      this.$store.commit("modelDesign/save_selectData", item); //传递选中元素
      this.$router.push(
        `/dataReceive/modelDesign/logicModel/property/${item.logicalModelId}/`
      );
    },
    // 取消
    resetDialogForm() {
      this.dialogVisible = false;
      this.analyseChartsVisible = false;
    },
  },
};
</script>
<style>
.dialog-warpper {
  padding: 20px;
}
</style>
<style lang="scss" scoped>
.content-warpper {
  display: flex;
  width: 100%;
  height: 92%;
  background: #fff;
  border-radius: 4px;
  overflow: hidden;
  border-bottom: 1px solid #dcdce6;
}
.left-tree {
  width: 15%;
  max-height: 80vh;
  background: #ffffff;
  overflow: auto;
  padding: 16px;
  border-right: 1px solid #dcdce6;
  border-radius: 4px 0;
  /deep/.el-input__inner {
    height: 32px;
    background: #ffffff;
    border-radius: 2px;
    border: 1px solid #e0e0e0;
  }
  .el-tree {
    padding-top: 16px;
    /deep/.el-tree-node {
      color: #333333;
      .el-tree-node__content {
        height: 30px;
      }
    }
    ::v-deep.el-tree-node.is-current > .el-tree-node__content {
      background-color: rgba(75, 113, 235, 0.16);
      border-left: 2px solid #4b71eb;
      color: #4b71eb;
    }
  }
}
.right-content {
  width: 85%;
}
.el-dialog-div {
  height: 60vh;
  overflow: hidden;
  // padding: 20px;
}
.server-main {
  position: relative;

  .search-form {
    background: #ffffff;
    padding: 16px 32px;
    border-top-right-radius: 4px;
    text-align: right;
    .el-form-item {
      margin-bottom: 0;
    }

    .form-input {
      margin-right: 32px;

      .el-input,
      .el-select {
        ::v-deep .el-input__inner {
          width: 240px;
          height: 32px;
          background: #ffffff;
          border-radius: 2px;
          border: 1px solid #e0e0e0;
        }
      }
    }
  }

  .server-table {
    margin-top: 0;
    padding: 0px 32px 24px;
    background: #ffffff;
    .little-circle-green {
      display: inline-block;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: #55c898;
      position: relative;
      right: 4px;
      top: -2px;
    }

    .little-circle-red {
      display: inline-block;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: #ee5564;
      position: relative;
      right: 6px;
      top: -2px;
    }

    .little-circle-yellow {
      display: inline-block;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: #dba839;
      position: relative;
      right: 6px;
      top: -2px;
    }
    .table-title {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .table-title-text {
        margin: 8px 0;
        font-size: 16px;
        line-height: 16px;
        border-left: 5px solid $--color-primary;
        padding-left: 10px;
      }

      margin-bottom: 10px;
    }
  }

  .ontology-edit {
    padding: 0 10px;

    & + .ontology-edit {
      border-left: 1px solid #dddddd;
    }
  }

  ::v-deep .el-table__append-wrapper {
    padding: 10px;
  }

  ::v-deep .el-dialog {
    width: 500px;
    border-radius: 5px;
  }

  .dialog-inline {
    ::v-deep .el-form-item__content {
      width: 300px;
    }

    button.el-button {
      background-color: $--color-primary !important;
      color: #fff !important;
      line-height: 1.2;
    }
  }
}
</style>
